package com.example.app.components


import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Button
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.PermissionStatus
import com.google.accompanist.permissions.rememberMultiplePermissionsState
import com.google.accompanist.permissions.shouldShowRationale
import java.util.jar.Manifest

@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun PermissionSample2() {
    var permissionStates = rememberMultiplePermissionsState(
        permissions = listOf(
            android.Manifest.permission.CAMERA,
            android.Manifest.permission.RECORD_AUDIO
        )
    )
    Scaffold(topBar = {
        TopAppBar() {
            Text(text = "ldn")
        }
    }) {
        Column(
            Modifier.fillMaxSize(),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            permissionStates.permissions.forEach { permissionState ->
                when(permissionState.permission) {
                    android.Manifest.permission.CAMERA -> {
                        when(permissionState.status) {
                            PermissionStatus.Granted -> {
                                Text(text = "同意授权")
                            }
                            // is 代表当前判断值是Denied时
                            is PermissionStatus.Denied -> {
                                Column {
                                    val text = if(permissionState.status.shouldShowRationale) {
                                        "拒绝授权(CAMERA)"
                                    } else {
                                        "同意授权(CAMERA)"
                                    }
                                    Text(text = text)
                                }
                            }
                        }
                    }
                    android.Manifest.permission.RECORD_AUDIO -> {
                        when(permissionState.status) {
                            PermissionStatus.Granted -> {
                                Text(text = "同意授权")
                            }
                            // is 代表当前判断值是Denied时
                            is PermissionStatus.Denied -> {
                                Column {
                                    val text = if(permissionState.status.shouldShowRationale) {
                                        "拒绝授权(RECORD_AUDIO)"
                                    } else {
                                        "同意授权(RECORD_AUDIO)"
                                    }
                                    Text(text = text)
                                }
                            }
                        }
                    }
                }
            }
            Button(onClick = {
                // 请求获取权限
                // 需要在AndroidManifest中添加权限声明
                permissionStates.launchMultiplePermissionRequest()
            }) {
                Text(text = "点击获取权限")
            }
        }
    }
}

@Preview(
    showSystemUi = true
)
@Composable
fun PermissionSample2Preview() {
    PermissionSample2()
}

